package main

import "fmt"

type MyStruct struct {
	MyName string
	Age    int
	Area   string
}

type MyOptionFunc func(*MyStruct)

func WithMyName(myName string) MyOptionFunc {
	return func(ms *MyStruct) {
		ms.MyName = myName
	}
}

func WithAge(age int) MyOptionFunc {
	return func(ms *MyStruct) {
		ms.Age = age
	}
}

func WithArea(area string) MyOptionFunc {
	return func(ms *MyStruct) {
		ms.Area = area
	}
}

var defaultMyStruct = &MyStruct{
	MyName: "Not set v2",
	Age:    0,
	Area:   "Not set V2",
}

//NewMyStruct 返回一个 MyStruct实例
func NewMyStruct() *MyStruct {
	return &MyStruct{
		MyName: "Not set",
		Age:    0,
		Area:   "Not set",
	}
}

func NewMyStructV2(opts ...MyOptionFunc) (ms *MyStruct) {
	ms = defaultMyStruct
	for _, v := range opts {
		v(ms)
	}

	return
}

func main() {
	myStruct1 := NewMyStruct()
	fmt.Printf("%v \n", myStruct1)

	myStruct2 := NewMyStructV2(
		WithMyName("哈哈哈哈"),
		WithAge(1000),
		WithArea("四川成都"),
	)

	fmt.Printf("%v \n", myStruct2)
}
